package com.tti.kiosofthercules;


import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.tti.kiosofthercules.TestUtils.sleep;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class StartMachineTest {

    @Rule
    public ActivityTestRule<SrcActivity> mActivityTestRule = new ActivityTestRule<>(SrcActivity.class);

    @Test
    public void startMachineTest() {
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.reg_start), withText("Scan QR Code"), isDisplayed()));
        appCompatButton.perform(click());

        sleep(10000);

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText.perform(click());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.signin_email), isDisplayed()));
        appCompatEditText2.perform(replaceText("bb@bb.com"), closeSoftKeyboard());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.signin_password), isDisplayed()));
        appCompatEditText3.perform(replaceText("111111"), closeSoftKeyboard());

        ViewInteraction appCompatButton3 = onView(
                allOf(withId(R.id.signin_signin_btn), withText("SIGN IN"), isDisplayed()));
        appCompatButton3.perform(click());

        sleep(5000);

//        Matcher<View> balView = allOf(withId(R.id.balance_acct),
//            childAtPosition(
//                allOf(withId(R.id.acnt_bal_container),
//                    childAtPosition(
//                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
//                        0)),
//                    1),
//            isDisplayed());
//
//        ViewInteraction textView = onView(balView);
//        String bal = getText(balView);
//
//        textView.check(matches(withText(bal)));
//
//        Double prevBal = Double.valueOf(bal.substring(1));

        ViewInteraction scanBtn = onView(
                allOf(withId(R.id.start_btn),isDisplayed()));
        scanBtn.perform(click());

        sleep(20000);

//        Matcher<View> balView2 = allOf(withId(R.id.balance_acct),
//            childAtPosition(
//                allOf(withId(R.id.acnt_bal_container),
//                    childAtPosition(
//                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
//                        0)),
//                1),
//            isDisplayed());
//
//        ViewInteraction textView2 = onView(balView2);
//        Double currBal = prevBal - 1;
//        String currBalStr = "$"+String.format("%.2f",currBal);
//        textView2.check(matches(withText(currBalStr)));
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }

    private String getText(Matcher<View> matcher){
        final String[] text = {null};
        onView(matcher).perform(new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(TextView.class);
            }

            @Override
            public String getDescription() {
                return "getting text from a TextView";
            }

            @Override
            public void perform(UiController uiController, View view) {
                TextView vw = (TextView) view;
                text[0] = vw.getText().toString();
            }
        });
        return text[0];
    }
}
